package com.dk.controller;

import com.alibaba.fastjson.JSONObject;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.data.Result;

import com.dk.domain.TCarDTO;
import com.dk.domain.TCarSeriesDTO;
import com.dk.em.ConstantEnum;
import com.dk.mapper.TCarMapper;
import com.dk.pojo.TCar;
import com.dk.service.ITCarService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/tCarController")
public class TCarController {

    @Autowired
    private ITCarService itCarService;

    @Autowired
    private TCarMapper tCarMapper;

    @ApiOperation(value = "车名搜索加分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value = "车辆名称",required = true,paramType = "query"),
            @ApiImplicitParam(name="startIndex",value = "开始下标",required = true,paramType = "query"),
            @ApiImplicitParam(name="pageSize",value = "每页条数",required = true,paramType = "query")
    })
    @RequestMapping(value = "getInfoByName",method = RequestMethod.POST)
    public Result<List<TCarDTO>> getInfoByName(@RequestParam String name, @RequestParam("startIndex") Integer startIndex, @RequestParam("pageSize") Integer pageSize){
      List<TCar> list = itCarService.getInfoByName(name,startIndex,pageSize);
      String jsonString = JSONObject.toJSONString(list);
      List<TCarDTO> tCarDTOList = JSONObject.parseArray(jsonString, TCarDTO.class);
      return Result.bulid(ConstantEnum.SUCCESS).setResult(tCarDTOList);
    }

    @ApiOperation(value = "根据标签查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="label",value = "标签",required = true)
    })
    @GetMapping("/sele")
    public Result<List<TCarDTO>> getInfoByTitle(@RequestParam String label){
       /* QueryWrapper Wrapper = new QueryWrapper<TCar>();
        Wrapper.like("label", label);
        List<TCar> list = tCarMapper.selectList(Wrapper);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(list);*/

        List<TCar> list = itCarService.getInfoByTitle(label);
        String tCarJsonString = JSONObject.toJSONString(list);
        List<TCarDTO> tCarDTOList = JSONObject.parseArray(tCarJsonString, TCarDTO.class);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(tCarDTOList);
    }

    @ApiOperation(value = "按上架时间升序查询分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name="startIndex",value = "开始下标",required = true,paramType = "query"),
            @ApiImplicitParam(name="pageSize",value = "每页条数",required = true,paramType = "query")
    })
    @GetMapping("/getInfoByTime")
    public Result<List<TCarDTO>> getInfoByTime(@RequestParam("startIndex") Integer startIndex, @RequestParam("pageSize") Integer pageSize){
        List<TCar> list = itCarService.getInfoByTime(startIndex,pageSize);
        String jsonString = JSONObject.toJSONString(list);
        List<TCarDTO> tCarDTOList = JSONObject.parseArray(jsonString, TCarDTO.class);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(tCarDTOList);
    }
}
